/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hoko.blur.processorsecond;

import ohos.media.image.ImagePacker;
import ohos.media.image.PixelMap;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class StackBlurManager {
    static final int EXECUTOR_THREADS = Runtime.getRuntime().availableProcessors();
    static final ExecutorService EXECUTOR = Executors.newFixedThreadPool(EXECUTOR_THREADS);

    /**
     * Original image
     */
    private final PixelMap _image;

    /**
     * Most recent result of blurring
     */
    private PixelMap _result;

    /**
     * Method of blurring
     */
    private final BlurProcess _blurProcess;

    /**
     * Constructor method (basic initialization and construction of the pixel array)
     *
     * @param image The image that will be analyed
     */
    public StackBlurManager(PixelMap image) {
        _image = image;
        _blurProcess = new JavaBlurProcess();
    }

    /**
     * process
     *
     * @param radius q
     * @return q
     */
    public PixelMap process(int radius) {
        _result = _blurProcess.blur(_image, radius);
        return _result;
    }

    /**
     * Returns the blurred image as a bitmap
     *
     * @return blurred image
     */
    public PixelMap returnBlurredImage() {
        return _result;
    }

    /**
     * Save the image into the file system
     *
     * @param path The path where to save the image
     */
    public void saveIntoFile(String path) {
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(path);
            ImagePacker imagePacker = ImagePacker.create();
            ImagePacker.PackingOptions options = new ImagePacker.PackingOptions();
            options.format = "image/png";
            options.quality = 90;
            imagePacker.initializePacking(out, options);
            imagePacker.addImage(_result);
            imagePacker.finalizePacking();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * Returns the original image as a bitmap
     *
     * @return the original bitmap image
     */
    public PixelMap getImage() {
        return this._image;
    }

    /**
     * PixelMap
     *
     * @param radius q
     * @return q
     */
    public PixelMap processNatively(int radius) {
        NativeBlurProcess blur = new NativeBlurProcess();
        _result = blur.blur(_image, radius);
        return _result;
    }
}